package pkd.vs.dao.model;

import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Staff {
	private int StaffID = -1;
	private String StaffName = "";
	private String StaffLastname = "";
	private String Username = "";
	private String Password = "";
	public int getStaffID() {
		return StaffID;
	}
	public void setStaffID(int staffID) {
		StaffID = staffID;
	}
	public String getStaffName() {
		return StaffName;
	}
	public void setStaffName(String staffName) {
		StaffName = staffName;
	}
	public String getStaffLastname() {
		return StaffLastname;
	}
	public void setStaffLastname(String staffLastname) {
		StaffLastname = staffLastname;
	}
	public String getUsername() {
		return Username;
	}
	public void setUsername(String username) {
		Username = username;
	}
	public String getPassword() {
		return Password;
	}
	public void setPassword(String password) {
		Password = password;
	}
	 private static java.sql.Connection  con = null;
     private final static String url = "jdbc:microsoft:sqlserver://";
     private final static String serverName= "localhost";
     private final static String portNumber = "1433";
     private final static String databaseName= "Virus";
     private final static String userName = "sa";
     private final static String password = "w,jcoj.0";
     private final static String selectMethod = "cursor"; 
     private static String getConnectionUrl(){
         return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
     }
     private static java.sql.Connection getConnection(){
         try{
              Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
              con = DriverManager.getConnection(getConnectionUrl(),userName,password);
              if(con!=null) System.out.println("Connection Successful!");
         }catch(Exception e){
              e.printStackTrace();
              System.out.println("Error Trace in getConnection() : " + e.getMessage());
        }
         return con;
     }
     public static Staff getStaffByLoginUserPassword(String as_userName, String as_password){
    	Staff o_staff = null;
    	DatabaseMetaData dm = null;
        ResultSet rs = null;
        try{
        	con= getConnection();
             if(con!=null){
                  dm = con.getMetaData();
                  System.out.println("Driver Information");
                  System.out.println("\tDriver Name: "+ dm.getDriverName());
                  System.out.println("\tDriver Version: "+ dm.getDriverVersion ());
                  System.out.println("\nDatabase Information ");
                  System.out.println("\tDatabase Name: "+ dm.getDatabaseProductName());
                  System.out.println("\tDatabase Version: "+ dm.getDatabaseProductVersion());
                  System.out.println("Avalilable Catalogs ");
                  rs = dm.getCatalogs();
                  Statement stm = con.createStatement();
                  stm.executeQuery("select Username username,Password pwd from dbo.Staff where Username = '"+as_userName+"' and Password = '"+as_password+"'");
                  while(rs.next()){
                       System.out.println("\tcatalog: "+ rs.getString(1));
                  } 
                  rs.close();
                  rs = null;
                  closeConnection();
             }else System.out.println("Error: No active Connection");
        }catch(Exception e){
             e.printStackTrace();
        }
        dm=null;
    	
    	o_staff = new Staff();
		return o_staff;
    	 
     }
     private static void closeConnection(){
         try{
              if(con!=null)
                   con.close();
              con=null;
         }catch(Exception e){
              e.printStackTrace();
         }
    }
}
